package com.jf.blog_jf04.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jf.blog_jf04.bean.Userinfo;
import com.jf.blog_jf04.service.UserinfoService;
import com.jf.blog_jf04.utils.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Component
@Slf4j
public class LoginUtils {

    private static HttpServletRequest request;
    private static StringRedisTemplate redisTemplate;
    private static UserinfoService userinfoService;

    /**
     * 静态属性注入
     *
     * @param request
     * @param redisTemplate
     * @param userinfoService
     */
    @Autowired
    public LoginUtils(HttpServletRequest request, StringRedisTemplate redisTemplate, UserinfoService userinfoService) {
        LoginUtils.request = request;
        LoginUtils.redisTemplate = redisTemplate;
        LoginUtils.userinfoService = userinfoService;
    }

    /**
     * 创建token
     *
     * @return
     */
    public static String createToken() {
        return md5(UUID.randomUUID().toString());
    }

    /**
     * md5加密
     *
     * @param str
     * @return
     */
    public static String md5(String str) {
        return DigestUtils.md5Hex(str);
    }

    /**
     * 创建密码
     *
     * @param password
     * @param slat
     * @return
     */
    public static String createPassword(String password, String slat) {
        return md5(password + "-" + slat);
    }

    /**
     * 创建盐值
     *
     * @return
     */
    public static String getSlat() {
        return UUID.randomUUID().toString().substring(0, 6);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    public static Userinfo getLoginUserinfo() {
        String token = request.getHeader(MyConstants.TOKEN_NAME);
        if (token == null) {
            log.debug("token不存在");
            throw new BusinessException("token不存在！");
        }
        String value = redisTemplate.opsForValue().get(MyConstants.TOKEN_KEY + token);
        if (value == null) {
            log.debug("token异常");
            throw new BusinessException("token异常！");
        }
        int userinfoId = Integer.parseInt(value);
        return userinfoService.getById(userinfoId);
    }

    /**
     * 前台页面根据href获取会员编号
     *
     * @return
     */
    public static Integer getVipId() {
        String href = request.getHeader(MyConstants.HREF_NAME);
        if (!StringUtils.hasText(href)) {
            return MyConstants.ADMIN_ID;
        }
        Userinfo userinfo = userinfoService.getOne(new QueryWrapper<Userinfo>().eq("href", href));
        if (userinfo == null) {
            throw new BusinessException(404, "你访问的用户不存在！");
        }
        return userinfo.getUserinfoId();
    }

    /**
     * 退出登录
     */
    public static void logout() {
        String token = request.getHeader(MyConstants.TOKEN_NAME);
        if (token != null) {
            redisTemplate.delete(MyConstants.TOKEN_KEY + token);
        }
    }

}
